export function loadMore(element: HTMLDivElement, callback: any): void {
    let timer;
    element.addEventListener('scroll', () => {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            let scrollHeight = element.scrollHeight;
            let clientHeight = element.clientHeight;
            let scrollTop = element.scrollTop;
            if (scrollTop + clientHeight + 20 >= scrollHeight) {
                callback()
            }
        }, 50);
    })
}
export function downRefresh(element: HTMLDivElement, callback: any) {
    let startY, distance, originalTop = element.offsetTop, $timer, startTime, startTop;
    element.addEventListener('touchstart', (event) => {
        if (element.scrollTop === 0) {
            startY = event.touches[0].pageY;
            startTime = Date.now();
            startTop = element.offsetTop;
            element.addEventListener('touchmove', touchMove)
            element.addEventListener('touchend', touchEnd)
        }    
        if ($timer) {
            clearInterval($timer)
        }
    })
    function touchMove(event) {
        if (Date.now() - startTime >= 50) {
            startTime = Date.now()
        } else {
            return
        }
        let pageY = event.touches[0].pageY;
        if (pageY > startY) {
            distance = pageY - startY;
            element.style.top = startTop + distance + 'px';
        }
    }
    function touchEnd(event) {
        $timer = setInterval(() => {
            if (originalTop <= element.offsetTop) {
                element.style.top = startTop + --distance + 'px';
            } else {
                element.style.top = originalTop + 'px';
                clearInterval($timer)
            }    
        }, 1)
        if (distance > 20) {
            callback()
        }
    }
}
